﻿Imports Microsoft.TeamFoundation
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports Microsoft.TeamFoundation.Framework.Client
Imports System.Net
Imports Microsoft.TeamFoundation.VersionControl.Client

Public Class WorkItemFieldHistory

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim configurationServerUri As New Uri(ModuleHost.GetServerAddress)
        Dim configurationServer As New TfsConfigurationServer(configurationServerUri, ModuleHost.GetCredentials())
        Dim tpcService As ITeamProjectCollectionService = configurationServer.GetService(Of ITeamProjectCollectionService)()
        Dim collections = tpcService.GetCollections()
        Dim collection As TfsTeamProjectCollection = configurationServer.GetTeamProjectCollection(collections(0).Id)
        Dim vcs As VersionControlServer = collection.GetService(GetType(VersionControlServer))


        Dim store As New WorkItemStore(collection)
        Dim wi = store.GetWorkItem(txtWorkItemID.Text)

        Dim FieldHistoryItems As New Dictionary(Of String, FieldHistory)
        For Each rev As Revision In wi.Revisions
            For Each f As Field In rev.Fields
                Dim orj As String = ""
                If f.OriginalValue IsNot Nothing Then
                    orj = f.OriginalValue.ToString
                End If
                Dim neww As String = ""
                If f.Value IsNot Nothing Then
                    neww = f.Value.ToString
                End If
                If orj <> neww Then
                    Dim fh As FieldHistory
                    If FieldHistoryItems.ContainsKey(f.Name) Then
                        fh = FieldHistoryItems(f.Name)
                    Else
                        fh = New FieldHistory() With {.FieldName = f.Name}
                        FieldHistoryItems.Add(fh.FieldName, fh)
                    End If
                    fh.Revisions.Add(rev)
                End If
            Next
        Next

        lstFieldChanges.DataSource = FieldHistoryItems.Values.OrderBy(Function(f) f.FieldName).ToList()
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lstFieldChanges.SelectedIndexChanged
        If lstFieldChanges.SelectedItem Is Nothing Then
            Exit Sub
        End If

        Dim fh As FieldHistory = lstFieldChanges.SelectedItem

        Dim s As String = ""
        For Each rev As Revision In fh.Revisions
            s += ControlChars.CrLf + ControlChars.CrLf + "Rev = " + rev.Index.ToString + " (" + rev.Fields("Changed By").Value.ToString() + " / " + rev.Fields("Changed Date").Value.ToString() + ") -----------------------------------------------------" + ControlChars.CrLf
            Dim f As Field = rev.Fields(fh.FieldName)
            Dim orj As String = ""
            If f.OriginalValue IsNot Nothing Then
                orj = f.OriginalValue.ToString
            End If
            Dim neww As String = ""
            If f.Value IsNot Nothing Then
                neww = f.Value.ToString
            End If
            If orj <> neww Then
                s += f.Name + " " + ControlChars.CrLf
                s += "       orj: " + orj + ControlChars.CrLf
                s += "       new: " + neww + ControlChars.CrLf
            End If
        Next

        MessageBox.Show(s)
    End Sub

    Private Class FieldHistory
        Public Sub New()
            Me.Revisions = New List(Of Revision)
        End Sub

        Public FieldName As String
        Public Revisions As List(Of Revision)
        Public OldValue As String
        Public NewValue As String

        Public Overrides Function ToString() As String
            Try
                Return Me.FieldName + " ( " + Revisions.Count().ToString + ") "
            Catch ex As Exception
                Return "???"
            End Try
        End Function

    End Class

    Private Sub pbHelp_Click(sender As System.Object, e As System.EventArgs) Handles pbHelp.Click
        Dim msg As String = "Work Item Field History" + ControlChars.CrLf + _
                            "The purpose of this module is to give work item history at field resolution." + ControlChars.CrLf + _
                            "" + ControlChars.CrLf + _
                            "TFS keeps track of changes to each field of work items and displays them in the history field but the history field is a one long continuous text field which makes it extremely difficult to track changes of a particular field." + _
                            "For example a work item history might include  100 revisions but the ""description"" field was changed on 3 times and it is very difficult to find who changed the description field through the history." + _
                            "This module goes through the work item's history and gathers all changes to all fields, displays them on a per field basis and lets the user to see how many times each field was changed, by whom and when."
        MessageBox.Show(msg, "Work Item Field History", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub btnSelect_Click(sender As System.Object, e As System.EventArgs) Handles btnSelect.Click
        If ModuleHost Is Nothing Then
            Throw New Exception("Module host can not set")
        End If

        Dim configurationServerUri As New Uri(ModuleHost.GetServerAddress)

        Dim configurationServer As New TfsConfigurationServer(configurationServerUri, ModuleHost.GetCredentials())
        Dim tpcService As ITeamProjectCollectionService = configurationServer.GetService(Of ITeamProjectCollectionService)()
        Dim collections = tpcService.GetCollections()
        Dim collection As TfsTeamProjectCollection = configurationServer.GetTeamProjectCollection(collections(0).Id)
        Dim vcs As VersionControlServer = collection.GetService(GetType(VersionControlServer))


        Dim store As New WorkItemStore(collection)

        Dim wiID As Integer = 0
        If TFSWorkItemSelector.SelectWorkItem(store, wiID) Then
            txtWorkItemID.Text = wiID.ToString()
        End If
    End Sub

End Class
